/*  
 ===============================================================================
 Chili is the jQuery code highlighter plugin
 ...............................................................................
 LICENSE: http://www.opensource.org/licenses/mit-license.php
 WEBSITE: http://noteslog.com/chili/
 
 Copyright 2007 / Andrea Ercolino
 ===============================================================================
 */

(function($) {
    ChiliBook = {version: "1.9", automatic: true, automaticSelector: "code", codeLanguage: function(a) {
            var b = $(a).attr("class");
            return b ? b : ''
        }, metadataSelector: "object.chili", recipeLoading: true, recipeFolder: "", stylesheetLoading: true, stylesheetFolder: "", defaultReplacement: '<span class="$0">$$</span>', replaceSpace: "&#160;", replaceTab: "&#160;&#160;&#160;&#160;", replaceNewLine: "&#160;<br/>", recipes: {}, queue: {}, preFixCopy: document.selection && document.selection.createRange, preContent: "", preElement: null};
    $.metaobjects = function(c) {
        c = $.extend({context: document, clean: true, selector: 'object.metaobject'}, c);
        function jsValue(a) {
            eval('value = ' + a + ";");
            return a
        }
        return $(c.selector, c.context).each(function() {
            var b = {target: this.parentNode};
            $('> param[@name=metaparam]', this).each(function() {
                $.extend(b, jsValue(this.value))
            });
            $('> param', this).not('[@name=metaparam]').each(function() {
                var a = this.name, value = jsValue(this.value);
                $(b.target).each(function() {
                    this[a] = value
                })
            });
            if (c.clean) {
                $(this).remove()
            }
        })
    };
    $.fn.chili = function(r) {
        var s = $.extend({}, ChiliBook, r || {});
        function cook(k, l) {
            function prepareStep(a, b) {
                var c = (typeof b.exp == "string") ? b.exp : b.exp.source;
                o.push({stepName: a, exp: "(" + c + ")", length: 1 + (c.replace(/\\./g, "%").replace(/\[.*?\]/g, "%").match(/\((?!\?)/g) || []).length, replacement: (b.replacement) ? b.replacement : s.defaultReplacement})
            }
            function knowHow() {
                var b = 1;
                var c = [];
                for (var i = 0; i < o.length; i++) {
                    var d = o[i].exp;
                    d = d.replace(/\\\\|\\(\d+)/g, function(m, a) {
                        return!a ? m : "\\" + (b + 1 + parseInt(a, 10))
                    });
                    c.push(d);
                    b += o[i].length
                }
                var e = '((?:\\s|\\S)*?)';
                var f = '((?:\\s|\\S)+)';
                var g = '(?:' + c.join("|") + ')';
                g = e + g + '|' + f;
                return new RegExp(g, (l.ignoreCase) ? "gi" : "g")
            }
            function escapeHTML(a) {
                return a.replace(/&/g, "&amp;").replace(/</g, "&lt;")
            }
            function replaceSpaces(b) {
                return b.replace(/ +/g, function(a) {
                    return a.replace(/ /g, n)
                })
            }
            function filter(a) {
                a = escapeHTML(a);
                if (n) {
                    a = replaceSpaces(a)
                }
                return a
            }
            function chef() {
                var i = 0;
                var j = 2;
                var c = arguments[1];
                var d = arguments[arguments.length - 3];
                if (!d) {
                    var e;
                    while (e = o[i++]) {
                        var f = arguments;
                        if (f[j]) {
                            var g = /(\\\$)|(?:\$\$)|(?:\$(\d+))/g;
                            var h = e.replacement.replace(g, function(m, a, K) {
                                var b = '';
                                if (a) {
                                    return"$"
                                } else if (!K) {
                                    return filter(f[j])
                                } else if (K == "0") {
                                    return e.stepName
                                } else {
                                    return filter(f[j + parseInt(K, 10)])
                                }
                            });
                            return filter(c) + h
                        } else {
                            j += e.length
                        }
                    }
                } else {
                    return filter(d)
                }
            }
            var n = s.replaceSpace;
            var o = [];
            for (var p in l.steps) {
                prepareStep(p, l.steps[p])
            }
            var q = k.replace(knowHow(), chef);
            return q
        }
        function checkCSS(a) {
            if (!s.queue[a]) {
                var e = document.createElement("link");
                e.rel = "stylesheet";
                e.type = "text/css";
                e.href = a;
                document.getElementsByTagName("head")[0].appendChild(e);
                s.queue[a] = true
            }
        }
        function makeDish(a, b) {
            var c = s.recipes[b];
            if (!c) {
                return
            }
            $el = $(a);
            var d = $el.text();
            if (!d) {
                return
            }
            d = d.replace(/\r\n?/g, "\n");
            var e = cook(d, c);
            if (s.replaceTab) {
                e = e.replace(/\t/g, s.replaceTab)
            }
            if (s.replaceNewLine) {
                e = e.replace(/\n/g, s.replaceNewLine)
            }
            a.innerHTML = e;
            if (ChiliBook.preFixCopy) {
                $el.parents().filter("pre").bind("mousedown", function() {
                    ChiliBook.preElement = this
                }).bind("mouseup", function() {
                    if (ChiliBook.preElement == this) {
                        ChiliBook.preContent = document.selection.createRange().htmlText
                    }
                })
            }
        }
        function getPath(a, b) {
            var c = {recipeFolder: s.recipeFolder, recipeFile: a + ".js", stylesheetFolder: s.stylesheetFolder, stylesheetFile: a + ".css"};
            var d;
            if (b && typeof b == "object") {
                d = $.extend(c, b)
            } else {
                d = c
            }
            return{recipe: d.recipeFolder + d.recipeFile, stylesheet: d.stylesheetFolder + d.stylesheetFile}
        }
        if (s.metadataSelector) {
            $.metaobjects({context: this, selector: s.metadataSelector})
        }
        this.each(function() {
            var b = this;
            var c = s.codeLanguage(b);
            if ('' != c) {
                var d = getPath(c, b.chili);
                if (s.recipeLoading || b.chili) {
                    if (!s.queue[d.recipe]) {
                        try {
                            s.queue[d.recipe] = [b];
                            $.getJSON(d.recipe, function(a) {
                                a.path = d.recipe;
                                s.recipes[d.recipe] = a;
                                if (s.stylesheetLoading) {
                                    checkCSS(d.stylesheet)
                                }
                                var q = s.queue[d.recipe];
                                for (var i = 0, iTop = q.length; i < iTop; i++) {
                                    makeDish(q[i], d.recipe)
                                }
                            })
                        } catch (recipeNotAvailable) {
                            alert("the recipe for '" + c + "' was not found in '" + d.recipe + "'")
                        }
                    } else {
                        s.queue[d.recipe].push(b)
                    }
                    makeDish(b, d.recipe)
                } else {
                    makeDish(b, d.recipe)
                }
            }
        });
        return this
    };
    $(function() {
        if (ChiliBook.automatic) {
            if (ChiliBook.elementPath) {
                ChiliBook.automaticSelector = ChiliBook.elementPath;
                if (ChiliBook.elementClass) {
                    ChiliBook.codeLanguage = function(a) {
                        var b = new RegExp("\\b" + ChiliBook.elementClass + "\\b", "gi");
                        var c = $(a).attr("class");
                        if (!c) {
                            return''
                        }
                        var d = $.trim(c.replace(b, ""));
                        return d
                    }
                }
            }
            $(ChiliBook.automaticSelector).chili()
        }
        if (ChiliBook.preFixCopy) {
            function preformatted(a) {
                if ('' == a) {
                    return""
                }
                do {
                    var b = (new Date()).valueOf()
                } while (a.indexOf(b) > -1);
                a = a.replace(/\<br[^>]*?\>/ig, b);
                var c = document.createElement('<pre>');
                c.innerHTML = a;
                a = c.innerText.replace(new RegExp(b, "g"), '\r\n');
                return a
            }
            $("body").bind("copy", function() {
                if ('' != ChiliBook.preContent) {
                    window.clipboardData.setData('Text', preformatted(ChiliBook.preContent));
                    event.returnValue = false
                }
            }).bind("mousedown", function() {
                ChiliBook.preContent = ""
            }).bind("mouseup", function() {
                ChiliBook.preElement = null
            })
        }
    })
})(jQuery);